module gui.gbutton;

import AGui.Backends.Allegro5.Allegro5;
import AGui.AGui;
import AGui.Widgets.Button.Button;

import share.container;
import mirshare, mirdefine;
import common.imagearray;

import allegro5.allegro;
import allegro5.allegro_image;


public class GButton : Button
{
    private Allegro5Image _button_img;

    private ImageArray m_images;
    private int m_nFocusImgIdx;         // ÀÌ¹ÌÁö °ü·Ã º¯¼öµé. 
    private int m_nClickImgIdx; 

    public override void paintBackground(PaintEvent paintEvent)
    {
        if (!isVisible()) return;

        auto state = getButtonState();


        if (state == ButtonStateEnum.CLICKED)
        {
            changeButtonImage(m_nClickImgIdx);
        }
        else
        {
            changeButtonImage(m_nFocusImgIdx);
        }

        if (_button_img !is null)
        {
            const float opacity = 1.0f;
            paintEvent.graphics().drawImage(_button_img, IntPoint.Zero, opacity);
        }
    }

    public void changeButtonImage(int buttonIndex)
    {
        if (_button_img !is null && m_images !is null)
        {
            ALLEGRO_BITMAP* bmp = m_images[buttonIndex];
            if (bmp !is null)
                _button_img.setBitmap(bmp, false);
            
            setSize(_button_img.getWidth(), _button_img.getHeight());
        }
    }

    public void createButton(ImageArray iarr, int nFocusImgIdx, int nClickImgIdx, int nStartX, int nStartY, bool focused = true)
    {
        m_images = iarr;
        m_nFocusImgIdx = nFocusImgIdx;
        m_nClickImgIdx = nClickImgIdx;

        setLocation(nStartX, nStartY);

        //if (focused)
       //     focus();
    }

    public this()
    {
        super();
        
        _button_img = new Allegro5Image();
    }

    public ~this()
    {
        if (_button_img !is null)
        {
            delete _button_img;
            _button_img = null;
        }
    }
}